package com.eight.cloud.core.utils.validation.validator;

import cn.hutool.core.util.StrUtil;
import com.eight.cloud.core.utils.format.Validate;
import com.eight.cloud.core.utils.validation.annotation.IsIdCard;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 身份证号码验证器
 *
 * @ClassName: IdCardValidator
 * @Author: TXC
 * @Date: 2024-10-29 14:47
 **/
public class IdCardValidator implements ConstraintValidator<IsIdCard, String> {
    private boolean required = false;
    @Override
    public void initialize(IsIdCard constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required) {
            return Validate.isIdCard(s);
        }else {
            if(StrUtil.isEmpty(s)) {
                return true;
            }else {
                return Validate.isIdCard(s);
            }
        }
    }
}
